package com.adam.example.repository;

import com.adam.example.entity.repository.Book;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface BookRepository extends ElasticsearchRepository<Book, Long> {

    @Highlight(fields = {
            @HighlightField(name = "title", parameters = @HighlightParameters(preTags = "<span style=\"color:red\">"
                    , postTags = "</span>")),
            @HighlightField(name = "description", parameters = @HighlightParameters(preTags = "<span style=\"color:red\">"
                    , postTags = "</span>"))
    })
    SearchHits<Book> findHighlightedBooksByTitleOrDescription(String title, String description);

    @Query("""
    {
        "bool": {
            "should":[
                {"match": {"title": "?0"}},
                {"match": {"description": "?1"}}
            ]
        }
    }
    """)
    SearchPage<Book> findByBoolQuery(String title, String description, Pageable pageable);

}
